package site.wetsion.employ.server.controller;

import java.math.BigInteger;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import site.wetsion.employ.server.pojo.Company;
import site.wetsion.employ.server.pojo.CompanyComment;
import site.wetsion.employ.server.pojo.CompanyDetail;
import site.wetsion.employ.server.pojo.CompanyProduct;
import site.wetsion.employ.server.pojo.Domain;
import site.wetsion.employ.server.pojo.Job;
import site.wetsion.employ.server.pojo.JobRemark;
import site.wetsion.employ.server.pojo.User;
import site.wetsion.employ.server.service.CommentRemarkService;
import site.wetsion.employ.server.service.CompanyService;
import site.wetsion.employ.server.service.JobService;
import site.wetsion.employ.server.util.Constant;
import site.wetsion.employ.server.util.Page;
import site.wetsion.employ.server.util.Result;
import site.wetsion.employ.server.vo.CommentPageVo;
import site.wetsion.employ.server.vo.CompanyListVo;
import site.wetsion.employ.server.vo.CompanyProductVo;
import site.wetsion.employ.server.vo.JobPageVo;
@Controller
public class CompanyController {
	
	@Autowired
	private CompanyService companyService;

	@Autowired
	private CommentRemarkService commentService;
	
	@Autowired
	private JobService jobService;
	
	/**
	 * 按行业来分页显示
	 * @param domain 行业id
	 * @param pageNum 页码
	 * @param model
	 * @return
	 */
	@GetMapping("company/domain/{domain}/{pageNum}")
	public String companyListByDomain(@PathVariable int domain,
			@PathVariable int pageNum, Model model){
		Page page = new Page(pageNum, 20);
		List<Company> companyList = companyService.searchCompanyByDomain(page, domain);
		int count = companyService.getCompanyCountByDomain(domain);
		String keyword = companyService.getDomainNameById(domain);
		page.setTotalRecord(count);
		model.addAttribute("companyList", companyList);
		model.addAttribute("page", page);
		model.addAttribute("domain", domain);
		//搜索的关键字，显示行业名称
		model.addAttribute("keyword", keyword);
		//显示类型，按行业显示
		model.addAttribute("showType", Constant.SHOWTYPE_DOMAIN);
		//显示类型的值，此处为行业id
		model.addAttribute("showTypeValue", domain);
		
		List<Domain> domainList = companyService.showAllDomain();
		model.addAttribute("domainList", domainList);
		return "companylist";
	}
	/**
	 * 默认显示公司列表，分页显示全部
	 * @param model
	 * @param pageNum 页码
	 * @return
	 */
	@GetMapping("company/list/{pageNum}")
	public String companyListByDefault(@PathVariable int pageNum, Model model){
		Page page = new Page(pageNum, 20);
		List<Company> companyList = companyService.showAllCompanyByPage(page);
		int count = companyService.getCompanyCount();
		page.setTotalRecord(count);
		model.addAttribute("companyList", companyList);
		model.addAttribute("page", page);
		//搜索的关键字，显示行业名称
		model.addAttribute("keyword", "全部");
		//显示类型，按默认显示全部
		model.addAttribute("showType", Constant.SHOWTYPE_DEFAULT);
		//显示类型的值，此处为空
		model.addAttribute("showTypeValue", null);
		
		List<Domain> domainList = companyService.showAllDomain();
		model.addAttribute("domainList", domainList);
		return "companylist";
	}
	
	/**
	 * 按搜索公司名关键字分页显示
	 * @param pageNum 页码
	 * @param keyword 公司名关键字
	 * @param model
	 * @return
	 */
	@GetMapping("company/search/{pageNum}")
	public String companyListBySearch(@PathVariable int pageNum, String keyword, Model model){
		Page page = new Page(pageNum, 20);
		List<Company> companyList = companyService.searchCompanyByName(page, keyword);
		int count = companyService.getSearchCountByName(keyword);
		page.setTotalRecord(count);
		model.addAttribute("companyList", companyList);
		model.addAttribute("page", page);
		//搜索的关键字，显示公司名
		model.addAttribute("keyword", keyword);
		//显示类型，按搜索公司名显示
		model.addAttribute("showType", Constant.SHOWTYPE_SEARCHNAME);
		//显示类型的值，此处为搜索关键字
		model.addAttribute("showTypeValue", keyword);
		return "companylist";
	}
	
	@GetMapping("company/detail/{id}")
	public String companyDetail(@PathVariable BigInteger id, Model model){
		Company company = companyService.getCompanyById(id);
		CompanyDetail detail = companyService.getCompanyDetailById(company.getCompany_detail());
		CompanyProduct product = companyService.getCompanyProductByCompanyId(id);
		CompanyProductVo vo = new CompanyProductVo();
		vo.setCompany(company);
		vo.setCompanyDetail(detail);
		vo.setProduct(product);
		Page page = new Page(1, 5);
		List<CompanyComment> commentList = commentService.getCommentsOfCompanyIdByPage(page, id);
		int count = commentService.getCommentCountOfCompany(id);
		page.setTotalRecord(count);
		
		Page jobPage = new Page(1, 1);
		List<Job> jobList = jobService.getJobsOfCompanyByPage(jobPage, id);
		int jobCount = jobService.getJobCountOfCompany(id);
		jobPage.setTotalRecord(jobCount);
		
		model.addAttribute("companyVo", vo);
		model.addAttribute("commentList", commentList);
		model.addAttribute("page", page);
		model.addAttribute("jobList", jobList);
		model.addAttribute("jobPage", jobPage);
		return "companydetail";
	}
	
	/** ajax分页显示公司下的职位列表*/
	@ResponseBody
	@GetMapping("company/detail/jobList/{companyId}/{pageNum}")
	public JobPageVo ajaxShowJobList(
			@PathVariable("companyId") BigInteger companyId, 
			@PathVariable("pageNum") int pageNum){
		Page jobPage = new Page(pageNum, 1);
		List<Job> jobList = jobService.getJobsOfCompanyByPage(jobPage, companyId);
		int jobCount = jobService.getJobCountOfCompany(companyId);
		jobPage.setTotalRecord(jobCount);
		JobPageVo vo = new JobPageVo();
		vo.setJobList(jobList);
		vo.setPage(jobPage);
		return vo;
	}
	
	/** ajax分页显示公司评论*/
	@ResponseBody
	@GetMapping("company/comment/{companyId}/{pageNum}")
	public CommentPageVo ajaxShowCommentList(
			@PathVariable("companyId") BigInteger companyId, 
			@PathVariable("pageNum") int pageNum){
		Page page = new Page(pageNum, 5);
		List<CompanyComment> commentList = commentService.getCommentsOfCompanyIdByPage(page, companyId);
		int count = commentService.getCommentCountOfCompany(companyId);
		page.setTotalRecord(count);
		CommentPageVo vo = new CommentPageVo();
		vo.setCommentList(commentList);
		vo.setPage(page);
		return vo;
	}
	
	/** 添加公司评论*/
	@PostMapping("company/addcomment")
	public String addCompanyComment(CompanyComment comment, HttpSession session){
		User user = (User) session.getAttribute("curr_user");
		if(user==null){
			return "redirect:/login";
		}
		comment.setComment_prefer(new BigInteger("0"));
		comment.setComment_user_id(user.getId());
		commentService.addCompanyComment(comment);
		return "redirect:/company/detail/"+comment.getComment_company_id();
	}
	
	/**点赞公司评论*/
	@ResponseBody
	@GetMapping("company/comment/prefer")
	public Result cancelCollectCompany(BigInteger id, HttpSession session){
		User user = (User) session.getAttribute("curr_user");
		if(user==null){
			return new Result(405, "登录失效");
		}
		boolean rs = commentService.addCommentPrefer(id);
		if(rs){
			return new Result(200, "点赞成功");
		}
		return new Result(400, "点赞失败");
	}
	
	@ResponseBody
	@GetMapping("company/autoshow")
	public CompanyListVo autoshow(String keyword){
		Page page = new Page(1, 100);
		CompanyListVo vo = new CompanyListVo();
		List<Company> companies = companyService.searchCompanyByName(page, keyword);
		vo.setCompanyList(companies);
		return vo;
	}
}
